package ynu.edu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import ynu.edu.domain.dto.ControlNetUnit;
import ynu.edu.entity.ControlNetRecord;
import ynu.edu.mapper.ControlNetRecordMapper;
import ynu.edu.service.ControlNetTemplateService;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

/**
 * ControlNet模板服务实现类
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class ControlNetTemplateServiceImpl implements ControlNetTemplateService {

    private final ControlNetRecordMapper controlNetRecordMapper;


    @Override
    public List<ControlNetRecord> getDefaultTemplates() {
        try {
            LambdaQueryWrapper<ControlNetRecord> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(ControlNetRecord::getTaskType, "template")
                   .eq(ControlNetRecord::getDeleted, 0)
                   .orderByAsc(ControlNetRecord::getUnitIndex);

            List<ControlNetRecord> templates = controlNetRecordMapper.selectList(wrapper);
            return templates;

        } catch (Exception e) {
            log.error("获取默认ControlNet模板记录失败", e);
            return List.of();
        }
    }



    /**
     * 将ControlNetRecord转换为ControlNetUnit
     */
    private ControlNetUnit convertToControlNetUnit(ControlNetRecord record) {
        ControlNetUnit unit = new ControlNetUnit();
        unit.setModule(record.getModule());
        unit.setModel(record.getModel());
        unit.setWeight(record.getWeight());
        unit.setResizeMode(record.getResizeMode());
        unit.setProcessorRes(record.getProcessorRes());
        unit.setThresholdA(record.getThresholdA());
        unit.setThresholdB(record.getThresholdB());
        unit.setGuidanceStart(record.getGuidanceStart());
        unit.setGuidanceEnd(record.getGuidanceEnd());
        unit.setPixelPerfect(record.getPixelPerfect());
        unit.setControlMode(record.getControlMode());
        unit.setInputImage(""); // 模板中的inputImage为空，需要用户提供
        
        return unit;
    }
} 